package com.gxdf.extendedcontrols.gxdfclient.components;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import com.gxdf.extendedcontrols.gxdfclient.data.Field;
import com.gxdf.extendedcontrols.gxdfclient.data.TypeDomain;
import com.gxdf.extendedcontrols.gxdfclient.data.ValidationMessage;
import com.gxdf.extendedcontrols.gxdfclient.data.ValidationResult;
import com.gxdf.extendedcontrols.gxdfclient.data.ValidationType;

import android.content.Context;
import android.text.InputFilter;
import android.text.InputType;
import android.text.method.PasswordTransformationMethod;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class GxdfEditText extends GxdfField {
	
	TextView label;
	EditText editText;
	

	public GxdfEditText(Context context, Field field) {
		super(context, field);		
		this.setOrientation(LinearLayout.VERTICAL);
		label= new TextView(context);
		label.setText(this.field.getLabelValue());
		editText = new EditText(context);
		editText.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));		
		String defaultValue = (field.getValues().size()==0 ? "" : field.getValues().get(0));
		editText.setText(defaultValue);	
		
		setMaxLength(editText);		
		setDomainType();		
		setType();
		
	}

	private void setType() {
		String typeName = field.getType().getName().toUpperCase();
		
		if(typeName.equals("PASSWORD")){
			editText.setTransformationMethod(new MyPasswordTransformationMethod());
			editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
		}
		
	}

	private void setMaxLength(EditText e) {
		InputFilter[] fArray = new InputFilter[1];
		fArray[0] = new InputFilter.LengthFilter(field.getLength());
		e.setFilters(fArray);
	}

	private void setDomainType() {
		
		TypeDomain type = field.getType().getTypeDomain();
		
		switch(type){
		
		case EMAIL : 
			editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
			break;
		case NUMBER:
			editText.setInputType(InputType.TYPE_CLASS_NUMBER);
			break;
		case DATE:
			editText.setInputType(InputType.TYPE_CLASS_DATETIME);
		case PASSWORD:
			editText.setTransformationMethod(new MyPasswordTransformationMethod());
			editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
			break;
		default:
			break;
		
		}
		
	}

	public void showlabel(boolean v){		
		label.setEnabled(v);
	}


	@Override
	public String getValue() {		
		return this.editText.getText().toString();		
	}


	@Override
	public void drawField() {
		// TODO Auto-generated method stub
		this.addView(label);
		this.addView(editText);	
	}
	
	public void saveValues(){		
		
		if(!editText.getText().toString().isEmpty()){
		
			List<String> newvalues = new ArrayList<String>();
			newvalues.add(0, editText.getText().toString());
			actualValues.clear();
			
			actualValues=newvalues;
		}
		else{
			actualValues.clear();
		}
		
	}
	
	public boolean isModified(){
		return actualValues.equals(getField().getValues());
	}
	
	public List<String> getActualValues(){
		return this.actualValues;
	}

	@Override
	public boolean errorFormat() {
		boolean invalid=false;
		
		if(!getField().getFormatValidation().isEmpty()){
			if (actualValues.size() > 0) {
				pattern = Pattern.compile(field.getFormatValidation());
				matcher = pattern.matcher(actualValues.get(0));
				invalid = !matcher.matches();
			}else{
				invalid = true;
			}
		}
		
		return invalid;
	}


	@Override
	public boolean isFieldvalid() {		
		return isValid;
	}

	@Override
	protected boolean errorLength() {
		return false;
	}
	
	
	public class MyPasswordTransformationMethod extends PasswordTransformationMethod {
	    @Override
	    public CharSequence getTransformation(CharSequence source, View view) {
	        return new PasswordCharSequence(source);
	    }

	    private class PasswordCharSequence implements CharSequence {
	        private CharSequence mSource;
	        public PasswordCharSequence(CharSequence source) {
	            mSource = source; // Store char sequence
	        }
	        public char charAt(int index) {
	            return '*'; // This is the important part
	        }
	        public int length() {
	            return mSource.length(); // Return default
	        }
	        public CharSequence subSequence(int start, int end) {
	            return mSource.subSequence(start, end); // Return default
	        }
	    }
	};

	



}
